<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<#include "../../base/_meta.html"> 
<@head>
<script type="text/javascript">
	var eee;
	var dialog = frameElement.dialog;
	$(function() {
		var leftFieldData = 
            [
			<#if fields?exists>
             <#list fields as field>
             { id: "${field.fieldid}", text: "${field.fieldname}"},
             </#list>
             </#if>
             ];
            var tableData =
            [
			<#if tables?exists>
             <#list tables as table>
            { id: "${table.tableid}", text: "${table.tablelname}"},
            </#list>
            </#if>
            ];
            $("#leftfield").ligerComboBox({ data: leftFieldData, isMultiSelect: false, isShowCheckBox: false,valueFieldID: 'lfield' });
            $("#rightfield").ligerComboBox({ data: null, isMultiSelect: false, isShowCheckBox: false ,valueFieldID: 'rfield'});
            $("#rtableid").ligerComboBox({
                data: tableData, isMultiSelect: false,valueFieldID: 'rtable',
                onSelected: function (newvalue){
                	$.ajax({
            			url:"/ddesign/table/gettableField?tableid="+newvalue,
            			success:function(data){
            				var newData = new Array();
            				for (var i = 0; i < data.length; i++){
            					var obj = new Object();
                                obj.id = data[i].fieldid;
                                obj.text = data[i].fieldname;
                                obj.pid=newvalue;
                                newData.push(obj);
                            }
            				liger.get("rightfield").setData(newData);
            	    	}
            		});
                }
            });
	});
	// Save
	function doSave(){
		var form = liger.get('form_edit');
       if(defineCheck()){
			$.ajax({
				url:"/ddesign/table/saveRelation",
				data:$("#form_edit").serialize(),
				success:function(msg){
		        	if(msg == "1"){
		        		parent.window.doSearch();// Rfresh grid of users
		        		$.ligerDialog.confirm('保存成功，继续添加或修改数据？', 
		        				function (yes) {
		        				if(yes){
		        					if($("#screen").val()=="A"){
		        						$("#leftfield").val("");
		        						$("#rtableid").val("");
		        						$("#rightfield").val("");
		        					}
		        				}else{
		        					dialog.close();
		        				}
		        			});
		        		
		        	}else{
		        		$.ligerDialog.error("保存失败，请确认信息无误后重试。"); 
		        	}
		    	},
		    	error: function(XMLHttpRequest, textStatus, errorThrown) {
		    		$.ligerDialog.error("服务器连接失败。"+errorThrown); 
	            }
			});
       }
	}
	function defineCheck(){
	 	var lfeild = $("#leftfield").val();
        var rtable = $("#rtableid").val();
        var rfeild = $("#rightfield").val();
        if(lfeild == ""){
        	$.ligerDialog.warn('请选择本表字段。');
    		return false;
        }
        if(rtable == ""){
        	$.ligerDialog.warn('请选择关系表。');
    		return false;
        }
        if(rfeild == ""){
        	$.ligerDialog.warn('请选择关系表字段。');
    		return false;
        }
        return true;
	}
</script>
<style type="text/css">
 body{ font-size:12px;}
 .l-table-edit {}
 .l-table-edit-td{ padding:4px;}
 .l-button-submit,.l-button-test{width:60px; float:center; margin-left:10px; padding-bottom:2px;}
 .l-verify-tip{ left:230px; top:120px;}
</style>
</@head>
<body style="overflow-x: hidden; padding: 2px;">
<form id="form_edit" data-validate="{}">
	<input type="hidden" name="screen" id="screen" value="${(screen)!}"/>
	<input type="hidden" name="tableid" id="tableid" value="${(tableid)!}"/>
	<table cellpadding="0" cellspacing="0" class="l-table-edit" >
		<tr>
			<td align="right" class="l-table-edit-td">本表字段:</td>
			<td align="left" class="l-table-edit-td" style="width: 160px">
				<input type="text" name="leftfield" id="leftfield"/>
			</td>
			<td align="left">当前表与其他表关联的字段</td>
		</tr>
		<tr>
			<td align="right" class="l-table-edit-td">关系表名:</td>
			<td align="left" class="l-table-edit-td" style="width: 160px">
				<input type="text" name="rtableid" id="rtableid"/>
			</td>
			<td align="left">与当前表关联的表</td>
		</tr>
		<tr>
			<td align="right" class="l-table-edit-td">关系字段:</td>
			<td align="left" class="l-table-edit-td" style="width: 160px">
				<input type="text" name="rightfield" id="rightfield"/>
			</td>
			<td align="left">与本表字段对应的字段</td>
		</tr>		
       <tr>
       <td align="center" class="l-table-edit-td" colspan="4">
			<input type="button" value="保存" class="l-button l-button-test" onclick="doSave();"/>
	   </td>
       </tr>
	</table>
</form>
<script type="text/javascript">
// if has radio,select,checkbox,put here for init

</script>
</body>
</html>